//------------------------------------------------------------------------------
//
// ...
// class: ClientReserve 
// author: 喵大斯( as3er.net )
// created: 2015/8/26
// copyright (c) 2015 喵大斯( aosnow@yeah.net )
//
//------------------------------------------------------------------------------

/// <reference path='../core/jquery.d.ts'/>
/// <reference path='../core/jquery.extend.d.ts'/>
/// <reference path='../core/page_core.d.ts'/>
/// <reference path='../core/utils.ts'/>
/// <reference path='../core/submitter.d.ts'/>
/// <reference path='../core/pageset.d.ts'/>

class ClientReserve
{
	//--------------------------------------------------------------------------
	//
	//  Class constructor
	//
	//--------------------------------------------------------------------------

	/**
	 * 客户预约数据显示控制器
	 * @param table 用于显示预约数据的表格 JQuery 对象
	 */
	constructor( table:JQuery )
	{
		this._table = table;
		this.initTable();
	}

	//--------------------------------------------------------------------------
	//
	//	Class properties
	//
	//--------------------------------------------------------------------------

	private _table:JQuery;

	//----------------------------------------
	// clientKey
	//----------------------------------------

	/** 客户 key */
	public clientKey;

	//----------------------------------------
	// reserveKey
	//----------------------------------------

	/** 预约记录 key */
	public reserveKey;

	//--------------------------------------------------------------------------
	//
	//	Class methods
	//
	//--------------------------------------------------------------------------

	private initTable():void
	{
		// 显示/隐藏客户预约数据资料
		var title:JQuery = this._table.find( "tr[rel='title']" );
		title.css( "cursor", "pointer" );
		title.click( ()=>
		{
			this.showOrHideDetailed();
		} );
	}

	/**
	 * 请求刷新当前设定的预约详情
	 * <p>警告：当设置了回调方法时，不会自动更新预约表单。</p>
	 * @param callback 当成功返回数据时的回调方法
	 */
	public requestInfo( callback?:Function )
	{
		if( empty( this.reserveKey ) )return;

		submitter.post( "client_reserve_get.php", { key: this.reserveKey }, ( d )=>
		{
			if( d.err == 0 )
			{
				if( $.isFunction( callback ) )
				{
					callback( d );
				}
				else
				{
					this.setData( d );
				}
			}
			else
			{
				infopad.err( d.msg );
			}
		}, "json" );
	}

	/**
	 * 设置数据填充预约数据表
	 * @param data
	 */
	public setData( data:any ):string
	{
		// 赋值数据
		if( !empty( data ) )
		{
			this.reserveKey = data.key;
			this.clientKey = data.client;

			// 重置 html 内容
			this.resetHtmlContent();

			// 填充数据
			this._table.find( "#re_date" ).html( data.date );
			this._table.find( "#re_time" ).html( data.time );

			if( !this._table.find( "#expired" ).exist() )
				this._table.find( "#re_date" ).parent().append( "<span class=\"expired unit hand\" id=\"expired\"></span>" );
			this._table.find( "#expired" ).html( data.note );

			this._table.find( "#re_region" ).html( data.region_ns );
			this._table.find( "#re_reserve_ns" ).html( data.reserve_ns );
			this._table.find( "#re_reception_ns" ).html( data.reception_ns );

			this._table.find( "#re_state" ).html( state_ns( parseInt( data.state ) ) );
			this._table.find( "#re_sms" ).html( sms_ns( parseInt( data.sms ) ) );
			this._table.find( "#re_needtime" ).html( data.needtime + " 小时" );
			this._table.find( "#re_remark" ).html( data.remark );
		}
		else
		{
			this.reserveKey = null;
			this.clientKey = null;
		}

		return this.reserveKey;
	}

	private resetHtmlContent():void
	{
		var h = '<tr>';
		h += '<th width="25%">预约时间</th>';
		h += '<td width="75%"><span class="stress unit hand" id="re_date"></span><span class="stress unit hand" id="re_time"></span></td>';
		h += '</tr>';
		h += '<tr>';
		h += '<th width="25%">预约中心</th>';
		h += '<td width="75%"><span class="stress" id="re_region"></span></td>';
		h += '</tr>';
		h += '<tr>';
		h += '<th width="25%">负责人</th>';
		h += '<td width="75%">登记预约老师：<span class="stress" id="re_reserve_ns"></span> - 接待老师：<span class="stress" id="re_reception_ns"></span></td>';
		h += '</tr>';
		h += '<tr rel="hide">';
		h += '<th width="25%">预约状态</th>';
		h += '<td width="75%">当前状态：<span class="confirmed unit hand" id="re_state"></span></td>';
		h += '</tr>';
		h += '<tr>';
		h += '<th width="25%">短信通知';
		h += '<p class="caption">预约客户后的短信通知状态</p>';
		h += '</th>';
		h += '<td width="75%"><span class="stress" id="re_sms"></span></td>';
		h += '</tr>';
		h += '<tr>';
		h += '<th width="25%">预计需要时间</th>';
		h += '<td width="75%"><span class="stress" id="re_needtime"></span></td>';
		h += '</tr>';
		h += '<tr>';
		h += '<th width="25%" style="padding: 15px 10px 15px 10px;">其它备注情况</th>';
		h += '<td width="75%"><div id="re_remark">无</div></td>';
		h += '</tr>';

		this._table.find( "tr:not(:first)" ).remove();
		this._table.append( h );
		update_row_bg( this._table );
	}

	//--------------------------------------------------------------------------
	//
	//  Event handlers
	//
	//--------------------------------------------------------------------------

	private showOrHideDetailed():void
	{
		var trs = this._table.find( "tr[rel='title'] ~ tr" );
		var show = trs.eq( 0 ).visible();
		show ? trs.hide() : trs.show();
	}
}